package com.tanhua.server.controller;

import com.tanhua.server.pojo.Answers;
import com.tanhua.server.service.TestSoulService;
import com.tanhua.server.service.TodayBestService;
import com.tanhua.server.vo.QuestionnaireVo;
import com.tanhua.server.vo.TestResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("testSoul")
public class TestSoulController {
    @Autowired
    private TestSoulService testSoulService;
    /**
     * 问卷列表
     * @return
     */
    @GetMapping
    public ResponseEntity<List<QuestionnaireVo>> SoulQuestionnaire() {
        try {
            List<QuestionnaireVo> questionnaireVos = this.testSoulService.SQuestionnaireLike();
            if (!CollectionUtils.isEmpty(questionnaireVos)) {
                return  ResponseEntity.ok(questionnaireVos);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 提交问卷
     *
     * @param answers
     */
    @PostMapping
    public ResponseEntity<String> testSoul(@RequestBody Map<String, List<Answers>> answers) {
        try {
            List<Answers> answersList = answers.get("answers");
            String id =  this.testSoulService.testSoul(answersList);
            System.out.println("reportId是"+id);
            return ResponseEntity.ok(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 测试结果
     * @param id
     * @return
     */
    @GetMapping("report/{id}")
    public ResponseEntity<TestResultVo> TestResult(@PathVariable("id") Long id) {
        try {
            TestResultVo testResultVo = this.testSoulService.testResult(id);
            if(testResultVo!=null){
                return ResponseEntity.ok(testResultVo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
